package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PriceSettingMapper;
import com.ruoyi.system.domain.PriceSetting;
import com.ruoyi.system.service.IPriceSettingService;

/**
 * 用户VIP 价格显示
Service业务层处理
 * 
 * @author DEREK
 * @date 2023-05-22
 */
@Service
public class PriceSettingServiceImpl extends ServiceImpl<PriceSettingMapper, PriceSetting> implements IPriceSettingService 
{
    @Autowired
    private PriceSettingMapper priceSettingMapper;

    /**
     * 查询用户VIP 价格显示

     * 
     * @param id 用户VIP 价格显示
主键
     * @return 用户VIP 价格显示

     */
    @Override
    public PriceSetting selectPriceSettingById(Long id)
    {
        return priceSettingMapper.selectPriceSettingById(id);
    }

    /**
     * 查询用户VIP 价格显示
列表
     * 
     * @param priceSetting 用户VIP 价格显示

     * @return 用户VIP 价格显示

     */
    @Override
    public List<PriceSetting> selectPriceSettingList(PriceSetting priceSetting)
    {
        return priceSettingMapper.selectPriceSettingList(priceSetting);
    }

    /**
     * 新增用户VIP 价格显示

     * 
     * @param priceSetting 用户VIP 价格显示

     * @return 结果
     */
    @Override
    public int insertPriceSetting(PriceSetting priceSetting)
    {
        return priceSettingMapper.insertPriceSetting(priceSetting);
    }

    /**
     * 修改用户VIP 价格显示

     * 
     * @param priceSetting 用户VIP 价格显示

     * @return 结果
     */
    @Override
    public int updatePriceSetting(PriceSetting priceSetting)
    {
        return priceSettingMapper.updatePriceSetting(priceSetting);
    }

    /**
     * 批量删除用户VIP 价格显示

     * 
     * @param ids 需要删除的用户VIP 价格显示
主键
     * @return 结果
     */
    @Override
    public int deletePriceSettingByIds(Long[] ids)
    {
        return priceSettingMapper.deletePriceSettingByIds(ids);
    }

    /**
     * 删除用户VIP 价格显示
信息
     * 
     * @param id 用户VIP 价格显示
主键
     * @return 结果
     */
    @Override
    public int deletePriceSettingById(Long id)
    {
        return priceSettingMapper.deletePriceSettingById(id);
    }

	@Override
	public BigDecimal qryScale(String type,BigDecimal amount) {
		BigDecimal scale = BigDecimal.ONE;
		PriceSetting one = getOne(new LambdaQueryWrapper<PriceSetting>().eq(PriceSetting::getPriceType, type).orderByDesc(PriceSetting::getId).last("limit 1")
				.select(PriceSetting::getId,PriceSetting::getCurrentPrice));
		if(null != one && null != one.getCurrentPrice() && one.getCurrentPrice().compareTo(BigDecimal.ZERO) == 1) {
			scale = new BigDecimal(one.getCurrentPrice().stripTrailingZeros().toPlainString());
		}
		BigDecimal countAmount = amount.multiply(scale);
		if(countAmount.compareTo(countAmount) == 1 ) {
			scale = BigDecimal.ONE;
		}
		return scale;
	}

	@Override
	public boolean authorityStatus(String priceType) {
		PriceSetting p = this.getOne(new LambdaQueryWrapper<PriceSetting>().eq(PriceSetting::getPriceType, priceType).select(PriceSetting::getId,PriceSetting::getEnabled));
		if(null != p && 1 == p.getEnabled()) {
			return true;
		}
		return false;
	}
}
